package calabashandroidplugin.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;

public class PathExtractor {

	public String getResource(String pathToResource){
		URL url = null;
		try {
		    url = new URL("platform:/plugin/CalabashAndroidPlugin/src/test.txt");
		    InputStream inputStream = url.openConnection().getInputStream();
		    BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
		    String inputLine;
		 
		    while ((inputLine = in.readLine()) != null) {
		        System.out.println(inputLine);
		    }
		 
		    in.close();
		 
		} catch (IOException e) {
		    e.printStackTrace();
		}
		return url.toString();
	}
	
	public String getProjectName() {
		IEditorPart editorPart = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow().getActivePage().getActiveEditor();
		String activeProjectName = null;

		if (editorPart != null) {
			IFileEditorInput input = (FileEditorInput) editorPart
					.getEditorInput();
			IFile file = input.getFile();
			IProject activeProject = file.getProject();
			activeProjectName = activeProject.getName();
		}
		return activeProjectName;
	}

	public String getWorkspacePath() {
		IPath configPath = ResourcesPlugin.getWorkspace().getRoot()
				.getLocation();
		String workspacePath = configPath.toString();
		return workspacePath;
	}

	public String getFullPath() {
		return getWorkspacePath() + "/" + getProjectName();
	}
}
